package com.lunhui.service.provider.common.threads;

import com.lunhui.cache.client.model.LoginUserDto;
import com.lunhui.service.lunhui.service.business.CacheService;
import com.lunhui.service.lunhui.service.business.UserService;
import com.lunhui.service.lunhui.service.business.enums.DatasourceEnum;
import com.lunhui.user.client.model.UserDto;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @author renminghe-kg
 */
public class UpdateUserInfoThread implements Callable<Map<DatasourceEnum, Object>> {
    private CacheService cacheService;
    private UserService userService;

    private LoginUserDto loginUserDto;
    private UserDto userDto;

    public UpdateUserInfoThread(CacheService cacheService, LoginUserDto loginUserDto) {
        this.cacheService = cacheService;
        this.loginUserDto = loginUserDto;
    }

    public UpdateUserInfoThread(UserService userService, UserDto userDto) {
        this.userService = userService;
        this.userDto = userDto;
    }

    @Override
    public Map<DatasourceEnum, Object> call() throws Exception {
        Map<DatasourceEnum, Object> resMap = new HashMap<>();

        if (cacheService != null) {
            cacheService.addLoginUserCache(loginUserDto);
            resMap.put(DatasourceEnum.valueOf("USER_CACHE_REDIS"), null);
        } else if (userService != null) {
            userService.updateUserInfo(userDto);
            resMap.put(DatasourceEnum.valueOf("USER_INFO_MYSQL"), null);
        }

        return resMap;
    }
}